package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.SearchPagePresenter;

public class ShowSearchView extends Activity implements OnClickListener {

	private Button SearchButton, oopsButton, backButton;
	private EditText SearchParams;
	private String SEARCH_PARAMETERS;
	private SearchPagePresenter myPresenter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_for_items);
		init();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_display_main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Initializes all of the views, and sets the listeners for the buttons
	 */
	private void init() {
		myPresenter = new SearchPagePresenter(this);

		SearchButton = (Button) findViewById(R.id.b_SearchPage);
		SearchButton.setOnClickListener(this);

		oopsButton = (Button) findViewById(R.id.oops_logout);
		oopsButton.setOnClickListener(this);

		backButton = (Button) findViewById(R.id.b_search_back);
		backButton.setOnClickListener(this);

		SearchParams = (EditText) findViewById(R.id.SearchParameters);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.b_SearchPage:
			SEARCH_PARAMETERS = SearchParams.getText().toString();
			intent = new Intent(this, DisplaySearchResultsView.class);
			intent.setPackage(getPackageName());
			intent.putExtra("Parameters", SEARCH_PARAMETERS);
			myPresenter.showSearchResults(intent);
			break;
		case R.id.oops_logout:
			intent = new Intent(this, LoginView.class);
			intent.setPackage(getPackageName());
			myPresenter.logout(intent);
			break;
		case R.id.b_search_back:
			intent = new Intent(this, UserMainMenuView.class);
			intent.setPackage(getPackageName());
			myPresenter.backToMainMenu(intent);
			break;
		}

	}

}
